﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Utils;
using System.IO;
using DevExpress.Web.ASPxTreeList;
using System.Drawing;
using System.Threading;
using DevExpress.Data;


public partial class UserControl_ControlReportDSForASM : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ThreadStart trh = new ThreadStart(LoadPeriod);
            ThreadStart trh1 = new ThreadStart(LoadMonthLy);

            Thread tr = new Thread(trh);
            Thread tr2 = new Thread(trh1);

            tr.Start();
            tr2.Start();

            tr.Join();
            tr2.Join();
           
        }
        else
        {
            if (Session["db"] != null)
            {
                DataTable db = (DataTable)Session["db"];
                ASPxTreeList1.DataSource = db;
                ASPxTreeList1.DataBind();
            }
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    void LoadPeriod()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getPeriod", CommandType.StoredProcedure);
            dt.BindDropDownList(db,DropQuater, "ID", "Des");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadMonthLy()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getMonthly", CommandType.StoredProcedure);
            dt.BindDropDownList(db, DropMonthy, "ID", "Des");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
              DataTable db=null;
            if (RadioMonthLy.Checked)
            {
               db= dt.Get_Data_DataTable(str, ref error, "K_getDSForASM", CommandType.StoredProcedure,
                   new SqlParameter("@Username", Session["user"].ToString()),
                   new SqlParameter("@YyyyMm", DropMonthy.SelectedValue.ToString()));
            }
            else
            {
                db = dt.Get_Data_DataTable(str, ref error, "K_getDSForASMQuarter", CommandType.StoredProcedure,
                  new SqlParameter("@Username", Session["user"].ToString()),
                  new SqlParameter("@QuyYyyyMm",DropQuater.SelectedValue.ToString()));
            }
           
          
            if (db.Rows.Count > 0)
            {


                ASPxTreeList1.DataSource = db;
                TinhTong();
                Session["db"] = db;

            }
            else
            {
                if (Session["db"] != null)
                {
                    Session["db"] = null;
                }
                ASPxTreeList1.DataSource = null;
                ShowMessageBox("Dữ liệu không có");
            }
            ASPxTreeList1.DataBind();
           
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            ASPxTreeList1.ExpandAll();
            string filename = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" + "DSForASM";
            ASPxTreeListExporter1.WriteXlsToResponse(filename);
        }
        catch
        {

        }
    }
    void TinhTong()
    {
       ASPxTreeList1.Summary.Clear();

       
        TreeListSummaryItem soluong = new TreeListSummaryItem();
        soluong.FieldName = "TDV";
        soluong.ShowInColumn = "TDV";
        soluong.SummaryType = DevExpress.Data.SummaryItemType.Count;
        soluong.DisplayFormat = "{0:Số lượng trình dược viên cấp dưới là:  ,0}" ;
        ASPxTreeList1.Summary.Add(soluong);
        

        TreeListSummaryItem doanhso = new TreeListSummaryItem();
        doanhso.FieldName = "TT";
        doanhso.ShowInColumn = "TT";
        doanhso.SummaryType = DevExpress.Data.SummaryItemType.Sum;
        doanhso.DisplayFormat = "{0:Doanh số nhóm chưa VAT  #,###}";
        ASPxTreeList1.Summary.Add(doanhso);

        TreeListSummaryItem doanhsoV = new TreeListSummaryItem();
        doanhsoV.FieldName = "TTV";
        doanhsoV.ShowInColumn = "TTV";
        doanhsoV.SummaryType = DevExpress.Data.SummaryItemType.Sum;
        doanhsoV.DisplayFormat = "{0:Doanh số nhóm đã VAT  #,###}";
        ASPxTreeList1.Summary.Add(doanhsoV);
    }
    //protected void ASPxTreeList1_CustomSummaryCalculate(object sender, TreeListCustomSummaryEventArgs e)
    //{
    //    TreeListNode node = e.Node.;
    //    switch (e.SummaryProcess)
    //    {

    //        case CustomSummaryProcess.Start:
    //            e.Value = (decimal)0;
               
    //            break;
    //        case CustomSummaryProcess.Calculate:

    //            e.Value = (decimal)e.Value + 1;
    //            break;
    //        case CustomSummaryProcess.Finalize:
    //                try
    //                {
                       
    //                    e.Value = "Trình dược viên tuyến dưới của:" +  node["TDV"].ToString() + " là " + e.Value.ToString();
    //                    break;
    //                }
    //                catch
    //                {
    //                    break;
    //                }
               
    //    }
    //  // e.Value="Trình dược viên tuyến dưới của" + e.Node["TDV"].ToString() + " là " + e.Value.ToString();
    //}
}